package com.whmnx.chitchat.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.utils.SPUtil;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
     * @author Administrator
     */
    public class WelcomeActivity extends Activity { 
    private ViewPager viewPager;

    private List<View> tabViews = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        viewPager = findViewById(R.id.view_pager);
        loadData();
    }


    protected void loadData() {

        //引入一个布局，三个共用，设置不同的背景图片
        LayoutInflater tabs = LayoutInflater.from(WelcomeActivity.this);

        View tab = tabs.inflate(R.layout.tab_001, null);
        ImageView imageView =  tab.findViewById(R.id.image);

        View tab1 = tabs.inflate(R.layout.tab_02, null);
        ImageView imageView1 =  tab1.findViewById(R.id.image);

        View tab2 = tabs.inflate(R.layout.tab_03, null);
        ImageView imageView3 =  tab2.findViewById(R.id.image3);

        View tab3 = tabs.inflate(R.layout.tab_end, null);
        ImageView imageView2 =  tab3.findViewById(R.id.image2);


        //添加到列表里
        tabViews.add(tab);
        tabViews.add(tab1);
        tabViews.add(tab2);
        tabViews.add(tab3);

        //设置适配器
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return tabViews.size();//
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(tabViews.get(position));
                //滑到最后一个页面，显示button
                //滑到最后一个页面，显示button
                if (position == tabViews.size() - 1) {
                    ImageView image = tabViews.get(position).findViewById(R.id.image2);
                    image.setOnClickListener(view -> {
                        if(SPUtil.getBoolean(Constants.WELCOME)){   //true 登录过
                            startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                        }else {
                            startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                        }
                        finish();
                    });
                }
                return tabViews.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(tabViews.get(position));
            }
        });
    }
}
